import tkinter as tk
from tkinter import messagebox

# 棋盘大小
BOARD_SIZE = 8
# 初始棋盘布局
INITIAL_BOARD = [
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 1, -1, 0, 0, 0],
    [0, 0, 0, -1, 1, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0]
]

class Othello:
    def __init__(self, master):
        self.master = master
        self.board = [row[:] for row in INITIAL_BOARD]
        self.current_player = 1  # 1: 黑子, -1: 白子
        self.create_widgets()
        self.update_board()

    def create_widgets(self):
        self.canvas = tk.Canvas(self.master, width=600, height=600)
        self.canvas.pack()
        self.canvas.bind("<Button-1>", self.on_click)

        self.status_label = tk.Label(self.master, text=f"当前玩家: {'黑子' if self.current_player == 1 else '白子'}")
        self.status_label.pack()

    def on_click(self, event):
        x, y = event.x, event.y
        col, row = x // 75, y // 75
        if self.is_valid_move(row, col):
            self.make_move(row, col)
            self.update_board()
            self.check_game_over()

    def is_valid_move(self, row, col):
        if self.board[row][col] != 0:
            return False

        directions = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
        for dr, dc in directions:
            r, c = row + dr, col + dc
            if 0 <= r < BOARD_SIZE and 0 <= c < BOARD_SIZE and self.board[r][c] == -self.current_player:
                while 0 <= r < BOARD_SIZE and 0 <= c < BOARD_SIZE and self.board[r][c] == -self.current_player:
                    r += dr
                    c += dc
                if 0 <= r < BOARD_SIZE and 0 <= c < BOARD_SIZE and self.board[r][c] == self.current_player:
                    return True
        return False

    def make_move(self, row, col):
        self.board[row][col] = self.current_player
        directions = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
        for dr, dc in directions:
            r, c = row + dr, col + dc
            to_flip = []
            while 0 <= r < BOARD_SIZE and 0 <= c < BOARD_SIZE and self.board[r][c] == -self.current_player:
                to_flip.append((r, c))
                r += dr
                c += dc
            if 0 <= r < BOARD_SIZE and 0 <= c < BOARD_SIZE and self.board[r][c] == self.current_player:
                for fr, fc in to_flip:
                    self.board[fr][fc] = self.current_player
        self.current_player = -self.current_player
        self.status_label.config(text=f"当前玩家: {'黑子' if self.current_player == 1 else '白子'}")

    def update_board(self):
        self.canvas.delete("all")
        for row in range(BOARD_SIZE):
            for col in range(BOARD_SIZE):
                x1, y1 = col * 75, row * 75
                x2, y2 = x1 + 75, y1 + 75
                self.canvas.create_rectangle(x1, y1, x2, y2, fill="green")
                if self.board[row][col] == 1:
                    self.canvas.create_oval(x1 + 5, y1 + 5, x2 - 5, y2 - 5, fill="black")
                elif self.board[row][col] == -1:
                    self.canvas.create_oval(x1 + 5, y1 + 5, x2 - 5, y2 - 5, fill="white")

    def check_game_over(self):
        if not any(self.is_valid_move(r, c) for r in range(BOARD_SIZE) for c in range(BOARD_SIZE)):
            self.current_player = -self.current_player
            if not any(self.is_valid_move(r, c) for r in range(BOARD_SIZE) for c in range(BOARD_SIZE)):
                self.show_result()

    def show_result(self):
        black_count = sum(row.count(1) for row in self.board)
        white_count = sum(row.count(-1) for row in self.board)
        winner = "平局"
        if black_count > white_count:
            winner = "黑子"
        elif white_count > black_count:
            winner = "白子"
        messagebox.showinfo("游戏结束", f"结果: {winner}\n黑子: {black_count}, 白子: {white_count}")
        self.master.quit()

if __name__ == "__main__":
    root = tk.Tk()
    game = Othello(root)
    root.mainloop()